package models

import (
	"gorm.io/gorm"
	"time"
)

// User 用户表
type User struct {
	ID        int64          `json:"id" gorm:"primarykey;comment:主键ID"`
	CreatedAt time.Time      `json:"created_at" gorm:"comment:创建时间"`
	UpdatedAt time.Time      `json:"updated_at" gorm:"comment:更新时间"`
	DeletedAt gorm.DeletedAt `json:"-" gorm:"index;comment:删除时间"`

	Username    string     `json:"username" gorm:"uniqueIndex;not null;size:50;comment:用户名"`
	Password    string     `json:"-" gorm:"not null;size:255;comment:密码"`
	Email       string     `json:"email" gorm:"uniqueIndex;size:100;comment:邮箱"`
	Phone       string     `json:"phone" gorm:"size:20;comment:手机号"`
	Avatar      string     `json:"avatar" gorm:"size:255;comment:头像"`
	Nickname    string     `json:"nickname" gorm:"size:50;comment:昵称"`
	Status      int        `json:"status" gorm:"default:1;comment:状态 1正常 2禁用"`
	LastLoginAt *time.Time `json:"last_login_at" gorm:"comment:最后登录时间"`
	LastLoginIP string     `json:"last_login_ip" gorm:"size:45;comment:最后登录IP"`

	// 关联
	Roles []Role `json:"roles" gorm:"many2many:sys_user_roles;"`
}

// TableName 指定表名
func (User) TableName() string {
	return "sys_users"
}

// Role 角色表
type Role struct {
	ID        int64          `json:"id" gorm:"primarykey;comment:主键ID"`
	CreatedAt time.Time      `json:"created_at" gorm:"comment:创建时间"`
	UpdatedAt time.Time      `json:"updated_at" gorm:"comment:更新时间"`
	DeletedAt gorm.DeletedAt `json:"-" gorm:"index;comment:删除时间"`

	Name        string `json:"name" gorm:"uniqueIndex;not null;size:50;comment:角色名称"`
	Code        string `json:"response" gorm:"uniqueIndex;not null;size:50;comment:角色代码"`
	Description string `json:"description" gorm:"size:255;comment:角色描述"`
	Status      int    `json:"status" gorm:"default:1;comment:状态 1正常 2禁用"`
	Sort        int    `json:"sort" gorm:"default:0;comment:排序"`

	// 关联
	Users       []User       `json:"users" gorm:"many2many:sys_user_roles;"`
	Permissions []Permission `json:"permissions" gorm:"many2many:sys_role_permissions;"`
}

// TableName 指定表名
func (Role) TableName() string {
	return "sys_roles"
}

// UserRole 用户角色关联表
type UserRole struct {
	UserID int64 `json:"user_id" gorm:"primarykey;comment:用户ID"`
	RoleID int64 `json:"role_id" gorm:"primarykey;comment:角色ID"`
}

// TableName 指定表名
func (UserRole) TableName() string {
	return "sys_user_roles"
}

// RolePermission 角色权限关联表
type RolePermission struct {
	RoleID       int64 `json:"role_id" gorm:"primarykey;comment:角色ID"`
	PermissionID int64 `json:"permission_id" gorm:"primarykey;comment:权限ID"`
}

// TableName 指定表名
func (RolePermission) TableName() string {
	return "sys_role_permissions"
}
